#! /bin/bash

VERSION="0.5.0"

red="\033[31m"
green="\033[32m"
yellow="\033[33m"
blue="\033[34m"
basic="\033[0m"

function PrintMainUsage {
    echo -e "Usage: znet command [ subcommand ]\n"
    echo -e "${green}Commands:$basic"
    echo -e "   ${blue}ip$basic"
    echo -e "       Desc: Get your ip from ${yellow}http://ifconfig.me$basic service"
    echo -e "   ${blue}wifi$basic"
    echo -e "       Desc: Network Manager CLI Wifi interface"
    echo -e "       Subcommands: type ${yellow}'znet wifi'$basic to get help"
    echo -e "   ${blue}stat$basic"
    echo -e "       Desc: Running | Stopped NetworkManager.service"
    echo -e "   ${blue}run$basic"
    echo -e "       Desc: Run NetworkManager.service by systemctl"
    echo -e "   ${blue}stop$basic"
    echo -e "       Desc: Stop NetworkManager.service by systemctl"
    echo -e "   ${blue}restart$basic"
    echo -e "       Desc: Stop service if running and start it by systemctl"

    exit 1
}


function PrintWifiUsage {
    echo -e "Usage: znet wifi [ subcommand ]"
    echo -e "${green}Subcommands:$basic"
    echo -e "   ${blue}list$basic"
    echo -e "       Desc: List all available wifi spots"
    echo -e "   ${blue}connect <Wifi SSID name> ${yellow}[args, ...]$basic"
    echo -e "       Desc: Connect to wifi spot with args"
    echo -e "       Args: Any wich able to nmcli"
    echo -e "       Example:"
    echo -e "           znet wifi ${blue}connect ${yellow}<qwer password 123>$basic"
    echo -e "           Will be converted to:"
    echo -e "           nmcli d wifi ${blue}connect ${yellow}<qwer password 123>$basic"
    echo -e "   ${blue}disconnect$basic"
    echo -e "       Desc: Disconnect from current wifi spot"
    
    exit 1
}

function StartNMService {
    echo -e "Starting NetworkManager.service..."
    systemctl start NetworkManager.service
    if [[ $? -eq 0 ]]; then
        echo -e "${green}Done.$basic"
    else
        echo -e "${red}Error!$basic"
    fi
}

function StopNMService {
    echo -e "Stopping NetworkManager.service..."
    systemctl stop NetworkManager.service
    if [[ $? -eq 0 ]]; then
        echo -e "${green}Done.$basic"
    else
        echo -e "${red}Error!$basic"
    fi
}

[[ -n $(nmcli 2>/dev/null) ]] && running="1" || running=""

function PrintStoppedError {
    echo -e "${red}Fatal!$basic NetworkManager is ${red}stopped$basic."
    exit 1
}

case $1 in
    ip )  
        [[ -z $running ]] && PrintStoppedError
        resp=$(curl -s "http://ifconfig.me" 2>/dev/null)
        [[ -n $resp ]] && echo $resp && exit 0
        echo -e "${red}None$basic"
        exit 1
        ;;
    wifi )
        [[ -z $running ]] && PrintStoppedError
        shift
        [[ -z $1 ]] && PrintWifiUsage && exit 1

        case $1 in
            list )
                nmcli d wifi rescan
                nmcli d wifi | znet_parse_nmcli --table
                ;;
            disconnect )
                nmcli device | grep "wifi " | awk '{print $1}' | xargs nmcli device disconnect
                ;;
            connect )
                shift
                nmcli d wifi connect $@
                ;;
        esac
        ;;
    run )
        if [[ -n $running ]]; then
            echo -e "${red}Fatal:$basic NetworkManager is already running!"
            exit 1
        fi
        StartNMService
        ;;
    stop )
        if [[ -z $running ]]; then
            echo -e "${red}Fatal:$basic NetworkManager is already stopped!"
            exit 1
        fi
        StopNMService
        ;;
    stat )
        if [[ -n $running ]]; then
            echo -e "NetworkManager: ${green}Alive$basic\n"
            echo "Connections:"

            for (( i=2; i < $(nmcli device | wc -l) + 1; i++ ))
            do
                proc=$(nmcli device | head -n$i | tail -1)
                if [[ -n $(echo $proc | grep "externally") ]]; then
                    color="\033[33m"
                elif [[ -n $(echo $proc | grep "disconnected") ]]; then
                    color="\033[31m"
                elif [[ -n $(echo $proc | grep "connected") ]]; then
                    color="\033[32m"
                fi
                echo -e "$color\t$proc\033[0m"
            done
            exit 0
        else
            echo -e "NetworkManager: ${red}Stopped$basic"
            exit 1
        fi
        ;;
    restart )
        if [[ -n $running ]]; then
            StopNMService
        fi
        StartNMService
        ;;
    * )
        PrintMainUsage
        ;;
esac
